package com.mall.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.result.Result;
import com.mall.system.entity.DictType;
import com.mall.system.service.DictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型控制器
 */
@Tag(name = "字典类型管理")
@Slf4j
@RestController
@RequestMapping("/system/dictTypes")
public class DictTypeController {
    
    @Autowired
    private DictTypeService dictTypeService;
    
    @Operation(summary = "字典类型列表")
    @GetMapping
    public Result<IPage<DictType>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            DictType dictType) {
        try {
            IPage<DictType> page = new Page<>(pageNum, pageSize);
            QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (dictType.getDictName() != null && !dictType.getDictName().isEmpty()) {
                queryWrapper.like("dict_name", dictType.getDictName());
            }
            if (dictType.getDictType() != null && !dictType.getDictType().isEmpty()) {
                queryWrapper.like("dict_type", dictType.getDictType());
            }
            if (dictType.getStatus() != null) {
                queryWrapper.eq("status", dictType.getStatus());
            }
            
            queryWrapper.orderByDesc("create_time");
            
            IPage<DictType> result = dictTypeService.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询字典类型列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据字典类型ID获取字典类型")
    @GetMapping("/{dictId}")
    public Result<DictType> getById(@PathVariable("dictId") Long dictId) {
        try {
            DictType dictType = dictTypeService.getById(dictId);
            if (dictType != null) {
                return Result.success(dictType);
            } else {
                return Result.failed("字典类型不存在");
            }
        } catch (Exception e) {
            log.error("查询字典类型异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "新增字典类型")
    @PostMapping
    public Result<Boolean> add(@RequestBody DictType dictType) {
        try {
            // 检查字典类型是否已存在
            QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_type", dictType.getDictType());
            if (dictTypeService.getOne(queryWrapper) != null) {
                return Result.failed("字典类型已存在");
            }
            
            boolean success = dictTypeService.save(dictType);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("新增字典类型失败");
            }
        } catch (Exception e) {
            log.error("新增字典类型异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新字典类型")
    @PutMapping
    public Result<Boolean> update(@RequestBody DictType dictType) {
        try {
            if (dictType.getDictId() == null) {
                return Result.failed("字典类型ID不能为空");
            }
            
            boolean success = dictTypeService.updateById(dictType);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新字典类型失败");
            }
        } catch (Exception e) {
            log.error("更新字典类型异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除字典类型")
    @DeleteMapping("/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Long> ids) {
        try {
            boolean success = dictTypeService.removeByIds(ids);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("删除字典类型失败");
            }
        } catch (Exception e) {
            log.error("删除字典类型异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "获取所有字典类型列表")
    @GetMapping("/list")
    public Result<List<DictType>> listAll() {
        try {
            QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1);
            queryWrapper.orderByAsc("dict_type");
            
            List<DictType> dictTypes = dictTypeService.list(queryWrapper);
            return Result.success(dictTypes);
        } catch (Exception e) {
            log.error("获取字典类型列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}